package inputAndOutputSynthetic;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 复制文件
 * @author yy
 *
 */

public class CopyFile {

	public static void main(String[] args) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			// 创建输入流对象：负责读取E:/我的青春谁做主.txt文件
			fis = new FileInputStream("E:/我的青春谁做主.txt");
			// 创建输出流对象：负责将读取的文件写入到D:/Program Files文件
			fos = new FileOutputStream("D:/Program Files/love.txt",true);
			// 创建中转数组words存储每次读取的内容
			byte [] words = new byte[1024];
			int len;
			// 通过循环实现文件读取
			while((len = fis.read(words)) != -1){
				fos.write(words, 2, len);
			}
			fos.flush();
			System.out.println("复制完成，请查看文件");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			// 关闭流
			try {
				if(fos != null){
					fos.close();
				}
				if(fis != null){
					fis.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
